/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameObject;

import AI.I3DChess;
import FrameWork.Models.GLModel;
import Manager.ResourceManager;
import Manager.ResourceManager.ECHESSPIECE;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Vector;
import javax.media.opengl.GL2;
import utils.*;

/**
 *
 * @author trong_000
 */
public class ChessPiece {
    
    public GLModel md; //Model 3D cua quan co
    public Vector3 position; //Vi tri (toa do OpenGL)
    public Vector3 prevPosition; 
    public POINT point; //To do hien tai trong mang cac quan co
    public int nameID; //1 quan co se co 1 nameID de phan biet va su dung trong NameStack 
    public boolean isChoose; //trang thai dang dc chon
    public boolean isMoving; //trang thai dang di chuyen

    
    public static final int FRAMES_NEED_TO_MOVE = 10;
    public static final float MAX_Y = 1.0f;
    public static final int RED_GENERAL = 0;
    public static final int RED_ADVISOR1 = 1;
    public static final int RED_ADVISOR2 = 2;
    public static final int RED_ELEPHANT1 = 3;
    public static final int RED_ELEPHANT2 = 4;
    public static final int RED_HORSE1 = 5;
    public static final int RED_HORSE2 = 6;
    public static final int RED_CHARIOT1 = 7;
    public static final int RED_CHARIOT2 = 8;
    public static final int RED_CANNON1 = 9;
    public static final int RED_CANNON2 = 10;
    public static final int RED_SOLDIER1 = 11;
    public static final int RED_SOLDIER2 = 12;
    public static final int RED_SOLDIER3 = 13;
    public static final int RED_SOLDIER4 = 14;
    public static final int RED_SOLDIER5 = 15;
    
    public static final int BLUE_GENERAL = 16;
    public static final int BLUE_ADVISOR1 = 17;
    public static final int BLUE_ADVISOR2 = 18;
    public static final int BLUE_ELEPHANT1 = 19;
    public static final int BLUE_ELEPHANT2 = 20;
    public static final int BLUE_HORSE1 = 21;
    public static final int BLUE_HORSE2 = 22;
    public static final int BLUE_CHARIOT1 = 23;
    public static final int BLUE_CHARIOT2 = 24;
    public static final int BLUE_CANNON1 = 25;
    public static final int BLUE_CANNON2 = 26;
    public static final int BLUE_SOLDIER1 = 27;
    public static final int BLUE_SOLDIER2 = 28;
    public static final int BLUE_SOLDIER3 = 29;
    public static final int BLUE_SOLDIER4 = 30;
    public static final int BLUE_SOLDIER5 = 31;
    
    double alphaY = 0;
    
    public ChessPiece(GLModel md, Vector3 pos){
        this.md = md;
        this.position = pos;
        prevPosition = new Vector3(pos.x,pos.y,pos.z);
        isChoose = false;
        isMoving = false;
    }
    
    public ChessPiece(GLModel md, POINT point){
        this.md = md;
        this.point = point;
        this.position = point.toVector3();
        prevPosition = point.toVector3();
        isChoose = false;
        isMoving = false;
    }
    
    public ChessPiece(int nameID, POINT point){
        md = ResourceManager.getInstance().getModelChessPiece((ECHESSPIECE.values()[nameID]));
        this.point = point;
        this.position = point.toVector3();
        prevPosition = point.toVector3();
        isChoose = false;
        isMoving = false;
    }
    public void update(float deltaTime){
        if(isMoving==false)
            prevPosition = new Vector3(position.x,position.y,position.z);
    }
    
    public void draw(GL2 gl){
        md.opengldraw(gl, position.x, position.y, position.z, 1.0f);
    }
    
    /**
     * Ham di chuyen toi 1 vi tri co toa do x,y,z (toa do OpenGL)
     * @param x
     * @param y
     * @param z 
     */
    
    public void moveTo(float x,float y,float z){
        if(Math.abs(position.x - x) < 0.01f && Math.abs(position.z - z) < 0.01f){
            isMoving = false;
            isChoose = false;
            prevPosition = new Vector3(position.x,position.y,position.z);
            return;
        }
        float x0 = prevPosition.x;
        float y0 = prevPosition.y;
        float z0 = prevPosition.z;

        float vX = (x-x0)/FRAMES_NEED_TO_MOVE;
        float vZ = (z-z0)/FRAMES_NEED_TO_MOVE;
        
        alphaY +=(float) 180.0f/FRAMES_NEED_TO_MOVE;

                       
        if(position.x != x)
            position.x += vX;
        if(position.z != z)
            position.z += vZ;
      
        position.y = (float) (MAX_Y*Math.sin(Math.toRadians(alphaY)));      
    }
    
    public void moveTo(Vector3 pos){
        moveTo(pos.x, pos.y, pos.z);
    }
    
    public void moveTo(POINT point){
        this.point = point;
        moveTo(point.toVector3());
    }

    public ArrayList<POINT> getListCanGo(int[][] map) {
        return null;
    }
    public boolean canMoveLeft(int[][] map) {
        return false;
    }
    public boolean canMoveUp(int[][] map) {
        return false;
    }
    public boolean canMoveRight(int[][] map) {
        return false;
    }
    public boolean canMoveDown(int[][] map) {
        return false;
    }
    public boolean isEmptyLocation(int[][] map, POINT p) {
        if (p.y < 1 || p.y > 9 || p.x < 1 || p.x > 10) {
            return false;
        }
        if (map[p.x][p.y] < 0 || map[p.x][p.y] > 31) {
            return true;
        }

        return false;
    }
    public boolean isOpponent(int id1, int id2) {
        if ( (id1 < 16 && id2 >= 16) || (id1 >= 16 && id2 < 16) ) {
            return true;
        }

        return false;
    }
}
